#include "stdafx.h"
#include "event_dispatcher.h"

CxEventDispatcher::CxEventDispatcher(UINT nId)
    : m_nDispatcherId(nId)
{
}

CxEventDispatcher::~CxEventDispatcher()
{
    m_ltListeners.clear();
}

BOOL CxEventDispatcher::RaiseEvent(UINT nEventId, WPARAM wParam, LPARAM lParam)
{
    BOOL bRet = FALSE;
    ListenersIt it = m_ltListeners.begin();
    for (; it != m_ltListeners.end(); ++it)
    {
        if ((*it)->OnEvent(m_nDispatcherId, nEventId, wParam, lParam))
        {
            bRet = TRUE;
            break;
        }
    }
    return bRet;
}

BOOL CxEventDispatcher::AddEventListener(IxEventListener* pListener)
{
    ListenersIt it = std::find(m_ltListeners.begin(), m_ltListeners.end(), pListener);
    if (it != m_ltListeners.end())
    {
        return FALSE;
    }

    m_ltListeners.push_front(pListener);
    return TRUE;
}

BOOL CxEventDispatcher::RemoveEventListener(IxEventListener* pListener)
{
    ListenersIt it = std::find(m_ltListeners.begin(), m_ltListeners.end(), pListener);
    if (it == m_ltListeners.end())
    {
        return FALSE;
    }

    m_ltListeners.erase(it);
    return TRUE;
}

BOOL CxEventDispatcher::ClearEventListener()
{
    m_ltListeners.clear();
    return TRUE;
}